import cMyswipe from "components/myswipe";
import { Toast } from 'vant';
import BarScan from 'components/bar-scan';

export default {
  components: {
    cMyswipe,
    BarScan
  },
  data() {
    return {
      swipeList: [],
      isScanning: false,
      brandsList: [
        {id: 37, name: '大疆', logo: require('@/assets/images/brands/大疆.jpg')},
        {id: 38, name: '帝瓦雷', logo: require('@/assets/images/brands/帝瓦雷.jpg')},
        {id: 31, name: '坚果', logo: require('@/assets/images/brands/坚果.jpg')},
        {id: 9, name: '洁碧', logo: require('@/assets/images/brands/洁碧.jpg')},
        {id: 10, name: '科沃斯', logo: require('@/assets/images/brands/科沃斯.jpg')},
        {id: 27, name: '乐高', logo: require('@/assets/images/brands/乐高.jpg')},
        {id: 12, name: '马歇尔', logo: require('@/assets/images/brands/马歇尔.jpg')},
        {id: 39, name: '美的', logo: require('@/assets/images/brands/美的.jpg')},
        {id: 23, name: '摩飞', logo: require('@/assets/images/brands/摩飞.jpg')},
        {id: 14, name: '韶音', logo: require('@/assets/images/brands/韶音.jpg')},
        {id: 21, name: '松下', logo: require('@/assets/images/brands/松下.jpg')},
        {id: 33, name: 'jbl', logo: require('@/assets/images/brands/jbl.jpg')}
      ],
      priceRanges: [],
      recommendGoodsList: []
    };
  },
  methods: {
    /**
     * 获取页面数据
     */
    getPageData: function() {
      let _url = document.location.href;
      const that = this;
      $http.post("home.index", {url: _url}).then(
        response => {
          const { data, result, msg } = response;
          if (result == 1) {
            const { banner, priceRanges, recommend_goods_list } = data;
            that.swipeList = banner || [];
            that.priceRanges = priceRanges || [];
            that.recommendGoodsList = recommend_goods_list || [];
          } else {
            that.$dialog.alert({ message: msg });
            Toast(msg || '获取页面数据失败！');
          }
        }
      );
    },

    /**
     * 搜索框获取焦点跳转到搜索页面
     */
    toSearch: function() {
      this.$router.push(this.fun.getUrl("search", { fromHome: 1 }));
    },

    /**
     * 扫码成功
     * @param {String} decodedText 扫码结果
     */
    scanOk: function(decodedText) {
      this.isScanning = false;
      this.$router.push(this.fun.getUrl("search", { fromHome: 1, keyword: decodedText }, {}));
    },

    /**
     * 扫码
     */
    scanCode: function() {
      this.isScanning = true;
    },

    /**
     * 品牌专区跳转
     * @param {Object} brand 品牌数据
     */
    goBrands: function(brand) {
      const { id } = brand;
      this.$router.push(this.fun.getUrl('brandgoods', { id }));
    },

    /**
     * 礼品专区跳转
     * @param {Object} priceRange 价格区间
     */
    goSearchGoods: function(priceRange) {
      let param = {};
      if (priceRange.max_price > 0) {
        param.top = priceRange.max_price;
      }
      if (priceRange.min_price > 0) {
        param.mini = priceRange.min_price
      }
      this.$router.push(this.fun.getUrl('search', {...param, fromHome: 1}));
    },

    /**
     * 跳转到商品详情
     * @param {Object} goods 商品信息
     */
    gotoGoodsDetail: function(goods) {
      const { id } = goods;
      this.$router.push(this.fun.getUrl('goods', { id }));
    }
  },
  mounted() {
    this.getPageData();
  },
  activated() {
    window.isInHome = 1; // 判断是否进入了首页
    this.isScanning = false;
  }
};